import {handlerGobackClick, hint} from "../../../utils/util";
import User from "../../../models/user";

const login = new User()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    phone:"",
    codeText:"获取验证码",
    isDisabled:false,
    authCode:"",
    serverAuthCode:""
  },
  inputPhone(e){
    this.setData({
      phone: e.detail.value
    })
  },
  inputAuthCode(e){
    this.setData({
      authCode: parseInt(e.detail.value)
    })
  },
  handlerGobackClick,
  goLogin(){
    wx.navigateTo({
      url:"/owner-app/pages/pw-login/index"
    })
  },
  goBefore(){
    wx.redirectTo({
      url:"/owner-app/pages/pw-login/index"
    })
  },
  pwNext(){
    if(!this.data.phone){
      hint("手机号不能为空")
      return
    }
    if(!this.data.authCode){
      hint("验证码不能为空")
      return;
    }
    // if(this.data.authCode!==this.data.serverAuthCode){
    //   hint("验证码错误")
    //   return;
    // }
    login.appCheckCode(this.data.phone,this.data.authCode)
        .then((res)=>{
          wx.navigateTo({
            url:"/owner-app/pages/input-pw-pw/index?phone="+this.data.phone
          })
        })
        .catch((err)=>{
          console.log(err);})
  },
  getCode(){
    if(this.data.phone===""){
      wx.showToast({title:"手机号不能为空",icon:'none',duration:2000})
      return
    }
    if(!this.data.isDisabled){
      login.registerGetAuthCode(this.data.phone)
          .then((res)=>{
            if(res.result.registerStatus === 0){
              // this.setData({
              //   serverAuthCode:res.result.checkCode
              // })
              this.setTime()
            }if (res.result.registerStatus === 1){
              wx.showToast({title:"手机号已注册，请登录",icon:'none',duration:2000})
            }})
          .catch((err)=>{
            console.log(err);})
    }else {
      wx.showToast({title:"请勿频繁点击",icon:'none',duration:2000})
    }
  },
  setTime(){
    let time = 60;
    let i = 0;
    let that = this;
    //1.在60秒之内，button上倒计时60秒，并显示
    //2.向controller请求验证码，并发送到指定的手机号上面
    let set = setInterval(()=> {
      this.setData({
        codeText:(--time)+"秒"
      })
      i++;
      //此变量用于标识只调用一次获取验证码的函数
      if (i==1) {
        //使获取验证码按钮不可点击
        this.setData({
          isDisabled:true
        })
      }
    }, 1000);

    //60秒之后需要做的事情
    setTimeout(()=> {
      //恢复获取验证码按钮可点击
      this.setData({
        isDisabled:false,
        codeText:"获取验证码",
      })
      clearInterval(set);//清除计时器
    }, time * 1000); //60000
  }
})
